//
//  CountDownViewController.m
//  GameBoard
//
//  Created by knight on 14-7-16.
//  Copyright (c) 2014年 knight. All rights reserved.
//

#import "CountDownViewController.h"

@interface CountDownViewController ()

@end

@implementation CountDownViewController


- (void) updateCountTime
{
    //times++;
    NSLog(@"CountDownTimes 执行多少次: %d",times);
    
    NSCalendar * calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *endTime = [[NSDateComponents alloc]init];
    //设置日期
    [endTime setYear:2014];
    [endTime setMonth:8];
    [endTime setDay:8];
    [endTime setHour:8];
    [endTime setMinute:8];
    [endTime setSecond:8];
    
    NSDate *toDate = [calendar dateFromComponents:endTime];
    NSLog(@"到期时间：%@",toDate);
    int units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:toDate options:0 ];
    
    
    
    //dateLabel.text = @"hell %d",[components year];
    
    [dateLabel setText:[NSString stringWithFormat:@"%d 年 %d 月 %d天 %d时 %d分 %d秒",[components year],[components month],[components day],[components hour],[components minute],[components second]]];
    
    //dateTime = [NSDate ];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    times = 100;
    
    //destinationTime = [NSDate dateWithTimeIntervalSinceNow:1700];
    
    destinationTime = [NSDate dateWithTimeIntervalSince1970:1453347200];
    
    NSLog(@"destinationTime 是多少：%@",destinationTime);
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountTime) userInfo:nil repeats:YES];
    
    //加入手势
    UITapGestureRecognizer * singleFingerOne = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleFingerEvent:)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [self.view addGestureRecognizer:singleFingerOne];
    
}

- (void)handleSingleFingerEvent: (id)sender
{
    NSLog(@"times 的值为100表示暂停了");
    if (times == 100) {
        [timer setFireDate:[NSDate distantFuture]];
        times = 0;
        NSLog(@"定时器暂停");
    }
    else{
        times = 100;
        [timer setFireDate:[NSDate distantPast]];
        NSLog(@"定时器重新启动");
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)dealloc
{
    //[super dealloc];
    
    [timer invalidate];
}

@end
